package com.leador.utils;

import android.text.TextUtils;

/**
 * Created by liuha on 2016/9/15.
 */
public class ByteUtils {
    private static final byte[] a = "0123456789abcdef".getBytes();
    public static String btteToString(byte[] paramArrayOfByte)
    {
        if (paramArrayOfByte == null) {
            return "";
        }
        byte[] arrayOfByte = new byte[2 * paramArrayOfByte.length];
        for (int i = 0; i < paramArrayOfByte.length; i++)
        {
            arrayOfByte[(2 * i)] = a[(paramArrayOfByte[i] >> 4 & 0xF)];
            arrayOfByte[(2 * i + 1)] = a[(paramArrayOfByte[i] & 0xF)];
        }
        return new String(arrayOfByte);
    }
    public static byte[] stringToByte(String paramString) {
        if (TextUtils.isEmpty(paramString)) {
            return new byte[0];
        }
        byte[] arrayOfByte = new byte[paramString.length() / 2];
        int i = 0;
        for (int j = 0; j < arrayOfByte.length; j++)
        {
            char c1 = paramString.charAt(i++);
            char c2 = paramString.charAt(i++);
            arrayOfByte[j] = ((byte)(a(c1) << 4 | a(c2)));
        }
        return arrayOfByte;
    }

    private static int a(char paramChar)
    {
        if (paramChar >= 'a') {
            return paramChar - 'a' + 10 & 0xF;
        }
        if (paramChar >= 'A') {
            return paramChar - 'A' + 10 & 0xF;
        }
        return paramChar - '0' & 0xF;
    }
}
